package chenji.itemeditor.itemeditor;

import java.util.UUID;

import chenji.itemeditor.nbt.base.NBTTagCompound;

public class Attribute {
	private AttributeType type;
	private AttributeSlot slot;
	private double amount;
	private int operation;
	private long uuidleast;
	private long uuidmost;
	
	public Attribute(){}
	public Attribute(AttributeType type, AttributeSlot slot, double amount) {
		this.type = type;
		this.slot = slot;
		this.amount = amount;
		this.operation = 0;
		this.uuidleast = UUID.randomUUID().getLeastSignificantBits();
		this.uuidmost = UUID.randomUUID().getMostSignificantBits();

	}

	public Attribute(AttributeType type, double amount) {
		this.type = type;
		this.slot = null;
		this.amount = amount;
		this.operation = 0;
		this.uuidleast = UUID.randomUUID().getLeastSignificantBits();
		this.uuidmost = UUID.randomUUID().getMostSignificantBits();
	}

	public Attribute setOperation(int operation) {
		this.operation = operation;
		return this;
	}

	public Attribute setUUID(UUID uuid) {
		this.uuidleast = uuid.getLeastSignificantBits();
		this.uuidmost = uuid.getMostSignificantBits();
		return this;
	}

	public Attribute setAmount(double amount) {
		this.amount = amount;
		return this;
	}

	public Attribute setAttributeType(AttributeType type) {
		this.type = type;
		return this;
	}

	public Attribute setAttributeSlot(AttributeSlot slot) {
		this.slot = slot;
		return this;
	}

	public AttributeSlot getAttributeSlot() {
		return this.slot;
	}

	public int getOperation() {
		return this.operation;
	}

	public double getAmount() {
		return this.amount;
	}

	public long getUUIDLeast() {
		return this.uuidleast;
	}

	public long getUUIDMost() {
		return this.uuidmost;
	}

	public NBTTagCompound getNBT() {
		NBTTagCompound n = new NBTTagCompound();
		n.setString("AttributeName", this.type.getType());
		n.setString("Name", this.type.getType());
		n.setDouble("Amount", this.amount);
		n.setInt("Operation", this.operation);
		n.setLong("UUIDLeast", this.uuidleast);
		n.setLong("UUIDMost", this.uuidmost);
		if (this.slot != null) {
			n.setString("Slot", this.slot.getName());
		}
		return n;
	}

	public String toString() {
		return this.getNBT().toString();
	}

	public Attribute loadFromNBTTagCompound(NBTTagCompound nbt) {
		String name = nbt.getString("AttributeName");
		this.type = AttributeType.loadFromString(name);
		if (this.type == null) {
			return null;
		}
		this.amount = nbt.getDouble("Amount");
		this.operation = nbt.getInt("Operation");
		String slot = nbt.getString("Slot");
		this.slot = AttributeSlot.loadFromString(slot);
		this.uuidleast = nbt.getLong("UUIDLeast");
		this.uuidmost = nbt.getLong("UUIDMost");
		return this;
	}
	public AttributeType getType() {
		return this.type;
	}
}
